﻿var currentNewUnfinishedSellAuctions;
var allProducts;
function jsonDateToNormalDate(jsonDate) {
    var milli = jsonDate.replace(/\/Date\((-?\d+)\)\//, '$1');
    var d = new Date(parseInt(milli));
    return d.toLocaleDateString();
}
function loadNextSellAuction(nextButton) {
    var startedId=$("#lastId").val();
    var data2Send = '{"startedId":' + startedId.toString() + '}';
    $.ajax({
        type: "POST",
        url: '/Auction/LoadNextSellAuctions',
        data: data2Send,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) { //call successfull
            $("#sellAuctionListDiv").append(data.content);
            if (data.lastId > 0) {
                $("#lastId").val(data.lastId);
            } else {
                $("#BtnNNextSellAuctions").remove();
                $("#lastId").remove();
            }
        },
        error: function (xhr) {
            //error occurred
        }
    });
}
function updateAuction(auction) {
    var auctionId = auction.AuctionId;
    var updateButton = $("#updateButton" + auctionId);
    if (updateButton.val().toUpperCase() == "UPDATE") {
        updateButton.val("Confirm Update");
        var newAuctionId = "sellAuction" + auctionId.toString();
        var auctionDiv = document.getElementById(newAuctionId);
        var pictureDiv = document.getElementById("picture" + auctionId);
        //var userName = document.getElementById("userName" + auctionId);
        var startedDate = document.getElementById("startedDate" + auctionId);
        var finishedDate = document.getElementById("finishedDate" + auctionId);
        var productName = document.getElementById("productName" + auctionId);
        var initialPrice = document.getElementById("initialPrice" + auctionId);
        var detail = document.getElementById("detail" + auctionId);
        startedDate.innerHTML = "<input id='" + startedDate.id + "Input' type='text' value='" + startedDate.innerHTML + "'/><img src='../../External/DatetimePicker/images2/cal.gif' onclick='javascript:NewCssCal(&apos;" + startedDate.id + "Input&apos;,&apos;&apos;,&apos;ddMMyyyy&apos;,&apos;dropdown&apos;,true,&apos;12&apos;,true)' style='cursor:pointer'/>";
        finishedDate.innerHTML = "<input id='" + finishedDate.id + "Input' type='text' value='" + finishedDate.innerHTML + "'/><img src='../../External/DatetimePicker/images2/cal.gif' onclick='javascript:NewCssCal(&apos;" + finishedDate.id + "Input&apos;,&apos;&apos;,&apos;ddMMyyyy&apos;,&apos;dropdown&apos;,true,&apos;12&apos;,true)' style='cursor:pointer'/>";
        var productNodeSelection = document.createElement('select');
        productNodeSelection.id = "productNodeSelection" + auctionId;
        var tempProductName = productName.innerHTML;
        productName.innerHTML = "";
        productName.appendChild(productNodeSelection);
        for (var i = 0; i < allProducts.length; i++) {
            var newProductOption = document.createElement('option');
            newProductOption.value = allProducts[i].ProductId;
            newProductOption.innerHTML = allProducts[i].ProductName;
            productNodeSelection.appendChild(newProductOption);
            if (allProducts[i].ProductName == tempProductName) {
                newProductOption.setAttribute('selected', true);
            }   
        }
        initialPrice.innerHTML = "<input type='number' id='" + initialPrice.id + "Input' type='text' value='" + initialPrice.innerHTML + "'/>";
        detail.innerHTML = "<textarea rows='4' cols='70' id='" + detail.id + "Input' >" + detail.innerHTML + "</textarea>";
    } else {
        var newAuctionId = "sellAuction" + auctionId.toString();
        var auctionDiv = document.getElementById(newAuctionId);
        var pictureDiv = document.getElementById("picture" + auctionId);
        var startedDate = document.getElementById("startedDate" + auctionId);
        var finishedDate = document.getElementById("finishedDate" + auctionId);
        var productName = document.getElementById("productName" + auctionId);
        var initialPrice = document.getElementById("initialPrice" + auctionId);
        var detail = document.getElementById("detail" + auctionId);
        var productNodeSelection = productName.childNodes[0];
        var selectedProductOption = productNodeSelection[productNodeSelection.selectedIndex];
        var selectedProductName = selectedProductOption.innerHTML;
        var selectedProductId = selectedProductOption.value;
        startedDate.innerHTML = document.getElementById("startedDate" + auctionId + "Input").value;
        finishedDate.innerHTML = document.getElementById("finishedDate" + auctionId + "Input").value;
        productName.removeChild(productNodeSelection);
        productName.appendChild(document.createTextNode(selectedProductName));
        initialPrice.innerHTML = document.getElementById("initialPrice" + auctionId + "Input").value;
        detail.innerHTML = document.getElementById("detail" + auctionId + "Input").value;
        var newAuction = JSON.parse(JSON.stringify(auction));
        newAuction.StartedDate = startedDate.innerHTML;
        newAuction.FinishedDate = finishedDate.innerHTML;
        for (var i = 0; i < allProducts.length; i++) {
            if (allProducts[i].ProductId == selectedProductId) {
                newAuction.Product = allProducts[i];
            }
        }
        newAuction.InitialPrice = initialPrice.innerHTML;
        newAuction.ProductDescription = detail.innerHTML;
        
        $.ajax({
            type: "POST",
            url: '/Auction/UpdateAuction',
            data: JSON.stringify(newAuction),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            processData: false,
            success: function (data) { //call successfull
                if (data.IsOk == 0) {
                    $("#ErrorPanelDiv" + auction.AuctionId).html("Thanh cong");
                    setTimeout(function () {
                        $("#ErrorPanelDiv" + auction.AuctionId).html("");
                    }, 2000);
                    auction = JSON.parse(JSON.stringify(newAuction));
                } else {
                    $("#ErrorPanelDiv" + auction.AuctionId).html(data.ErrorMessage);
//                    setTimeout(function () {
//                        $("#ErrorPanelDiv" + auction.AuctionId).html("");
//                    }, 2000);
                    $("#startedDate" + auction.AuctionId).html(jsonDateToNormalDate(auction.StartedDate));
                    $("#finishedDate" + auction.AuctionId).html(jsonDateToNormalDate(auction.FinishedDate));
                    $("#productName" + auction.AuctionId).html(auction.ProductName);
                    $("#initialPrice" + auction.AuctionId).html(auction.InitialPrice);
                    $("#detail" + auction.AuctionId).html(auction.ProductDescription);
                }
            },
            error: function (xhr) {
                //error occurred
            }
        });
        $("#updateButton" + auction.AuctionId).val("Update");
    }
}

function deleteAuction(auctionId) {
    var data2send='{"auctionId":' + auctionId.toString() + '}';
    $.ajax({
        type: "POST",
        url: '/Auction/DeleteAuction',
        data: data2send,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        //processData: false,
        success: function (data) { //call successfull
            if (data.IsOk == 0) {
                $("#ErrorPanelDiv" + auctionId).html("Thanh cong");
                setTimeout(function () {
                    $("#ErrorPanelDiv" + auctionId).html("");
                }, 2000);
                $("#sellAuction" + auctionId).remove();
            } else {
                $("#ErrorPanelDiv" + auctionId).html("Loi");
                setTimeout(function () {
                    $("#ErrorPanelDiv" + auctionId).html("");
                }, 2000);
            }
        },
        error: function (xhr) {
            //error occurred
        }
    });
}

function slideToggleNewSellAuctionDiv() {
    $("#newSellAuctionDiv").slideToggle("slow");
}
function submitAddNewSellAuction() {
    alert("submitAddNewSellAuction");
}